package com.zahb.qadx.util;

import android.text.TextUtils;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.zahb.qadx.base.BaseApplication;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * CompatHelper
 * Created by zhoudawei on 2017/6/13.
 */

public final class CompatHelper {

    public static int getColor(@ColorRes int colorId) {
        return ContextCompat.getColor(BaseApplication.getContext(), colorId);
    }

    public static boolean isEmpty(@Nullable String text) {
        return TextUtils.equals("null", text) || TextUtils.isEmpty(text);
    }

    public static boolean isEmpty(@NonNull TextView textView) {
        return isEmpty(getString(textView));
    }

    public static String getString(@NonNull TextView textView) {
        return textView.getText().toString().trim();
    }

    private static SimpleDateFormat sSimpleDateFormatDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    private static SimpleDateFormat sSimpleDateFormatDate = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);

    public static String getDateString(String dateTime) {
        try {
            Date date = sSimpleDateFormatDateTime.parse(dateTime);
            if (date != null)
                return sSimpleDateFormatDate.format(date);
            else
                return "";
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
    }
}
